package com.chap14;

/**
 * Created by IntelliJ IDEA.
 * User: bj910
 * Date:2023-04-13
 * Time: 6:03
 * To change this template use File | Settings | File Templates.
 */
public class VisibilityTest {
    private boolean flag = true;

    public synchronized void refresh(){
        flag = false;
        System.out.println(Thread.currentThread().getName()+"修改flag："+flag);
    }

    public void load(){
        System.out.println(Thread.currentThread().getName()+"开始执行....");
        while (flag){
            //todo 业务逻辑：加载数据
        }
        System.out.println(Thread.currentThread().getName()+"数据加载完成，跳出循环");
    }

    public static void main(String[] args) throws InterruptedException {
        VisibilityTest test = new VisibilityTest();
        Thread threadA = new Thread(()->{
           test.load();
        },"threadA");
        threadA.start();

        Thread.sleep(1000);

        Thread threadB = new Thread(()->test.refresh(),"threadB");
        threadB.start();
    }
}
